package com.store.service.lt;

import com.store.entity.Resource;
import com.store.entity.Role;
import com.store.entity.User;
import com.store.mapper.lt.ResourceMapper;
import com.store.mapper.lt.RoleMapper;
import com.store.mapper.lt.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    public List<User> queryUsers() {
        return userMapper.selectAll();
    }

    public User queryUserByUsername(String principal) {
        return userMapper.selectUserByUsername(principal);
    }

    public int updatePwd(String username,String password){
        User user = queryUserByUsername(username);
        System.out.println("修改密码查询到的user:"+user);
        System.out.println("修改密码查询到的颜值:"+user.getSalt());
        //
        Object salt = ByteSource.Util.bytes(user.getSalt());
        // 密码加密
        Object newPwd = new SimpleHash("SHA1", password, salt, 256);
        System.out.println("新加密的密码:" + newPwd);
        String newpwd = newPwd.toString();
        int i = userMapper.updateUserPassword(username,newpwd);
        return i;
    }

    public int queryUserByPhone(String phone) {
        int i = userMapper.selectUserByPhone(phone);
        System.out.println("返回值："+i);
        return i;
    }

    public User queryUserByPhone2(String phone) {
        User user = userMapper.selectUserByPhone2(phone);
        System.out.println("返回值："+user.toString());
        return user;
    }


    public List<Role> queryRolesByUser(User user) {
        return roleMapper.queryRolesByUser(user);
    }

    /**
     * 获取所有的字符串
     * @param user
     */
    public List<String> queryRolesNameByUser(User user) {
        List<Role> roles = roleMapper.queryRolesByUser(user);
        List<String> list = new ArrayList<>();
        for (Role role : roles) {
            list.add(role.getRolename());
        }
        return list;
    }

    public List<Resource> queryResourcesByRole(Role role) {
        return resourceMapper.queryResourcesByRole(role);
    }

    //查询所有的权限
    public Set<String> queryAuthority(User paramUser){
        //1.用户有了
        User user = queryUserByUsername(paramUser.getUsername());
        //2.查询用户的所有角色
        List<Role> roles = queryRolesByUser(user);
        //3.通过角色查询所有的权限
        //将所有的权限存储起来
        Set<String> ress = new HashSet<>();
        for (Role role : roles) {
            List<Resource> resources = queryResourcesByRole(role);
            for (Resource resource : resources) {
                String permission = resource.getResurl();
                if (StringUtils.isBlank(permission)) {
                    continue;
                }
                permission = StringUtils.trim(permission);
                permission = permission.replaceAll("[\\/\\\\]", ":");
                permission = permission.substring(1);
                ress.add(permission);
            }
        }
        return ress;
    }

    /**
     * 查询所有的角色
     */
    public List<Role> queryRoles() {
        return roleMapper.selectAll();
    }

    /**
     * 查询所有的资源
     * @return
     */
    public List<Resource> queryResources() {
        return resourceMapper.selectAll();
    }

    public int addResources(List<Resource> resourcesAdd) {
        int i = 0;
        for (Resource resource : resourcesAdd) {
           i += resourceMapper.insertSelective(resource);
        }
        return i;
    }

    public List<Resource> queryResourcesByRoleId(int id) {
        return resourceMapper.queryResourcesByRoleId(id);
    }

    public boolean addUser(User user) {
        return userMapper.addUser(user.getId() ,user.getUsername(),
                user.getPassword(),user.getPhone(),user.getStatus(),user.getSalt()) ;
    }

    public boolean addAuthority(Integer roleId, Integer resourcesId) {
        return roleMapper.addAuthority(roleId, resourcesId);
    }

    public boolean deleteAuthority(Integer roleId, Integer resourcesId) {
        return roleMapper.deleteAuthority(roleId, resourcesId);
    }

    public boolean updateUserByLock(Integer userId, String status) {
        return userMapper.updateUserByLock(userId, status);
    }

    public boolean addUserRole(Integer userId, Integer roleId) {
        return roleMapper.addUserRole(userId, roleId);
    }

    public boolean deleteUserRole(Integer userId, Integer roleId) {
        return roleMapper.deleteUserRole(userId, roleId);
    }

    public boolean addRole(Role role) {
        return roleMapper.insertSelective(role) > 0;
    }


}
